<?php


namespace app\mobile\controller;


use think\Db;
use think\facade\Request;

class App extends LoggingBase
{

    /**
     * 心跳
     */
    public function mb_HeartBeat()
    {
        $UserID = $this->m_User->ID;
        $Data['Call'] = Db::table('user_calls')->field('uc.ID, c.Phone, uc.ClientID')->alias('uc')->where("uc.UserID='$UserID' AND uc.IsDeleted='0' AND uc.IsCalled='0'")->leftJoin('client c','c.ID=uc.ClientID')->limit(1)->select();
        $Data['SMS'] = Db::table('user_sms')->field('us.ID, c.Phone, us.Content')->alias('us')->where("us.UserID='$UserID' AND us.IsDeleted='0' AND us.IsSent='0'")->leftJoin('client c','c.ID=us.ClientID')->limit(1)->select();
        mb_OutMessage(ErrCode_Normal, '', $Data);
    }

    /**
     * 设置已拨打
     */
    public function mb_SetIsCalled()
    {
        $ID = Request::has('ID') ? Request::param('ID') : 0;
        if (!$ID) {
            mb_OutMessage(ErrCode_Toast, '参数错误');
        }
        Db::startTrans();
        try {
            Db::table('user_calls')->where("ID='$ID'")->update(['IsCalled'=>1]);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            mb_OutMessage(ErrCode_Toast, $e->getMessage());
        }
        mb_OutMessage(ErrCode_Normal,'设置成功');
    }

    /**
     * 设置已发短信
     */
    public function mb_SetIsSent(){
        $ID = Request::has('ID') ? Request::param('ID') : 0;
        if (!$ID) {
            mb_OutMessage(ErrCode_Toast, '参数错误');
        }
        Db::startTrans();
        try {
            Db::table('user_sms')->where("ID='$ID'")->update(['IsSent'=>1]);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            mb_OutMessage(ErrCode_Toast, $e->getMessage());
        }
        mb_OutMessage(ErrCode_Normal,'设置成功');
    }

    /**
     * 保存通话记录
     */
    public function mb_SetCallRecord()
    {
        $UserID = $this->m_User->ID;
        $ClientID = Request::has('ClientID') ? Request::param('ClientID') : '';
        $Type = Request::has('Type') ? Request::param('Type') : '';
        $TalkTime = Request::has('TalkTime') ? Request::param('TalkTime') : 0;

        $Status = '0';
        switch ($Type) {
            case '呼入':
                $Status = '0';
                break;
            case '呼出':
                $Status = '0';
                break;
            case '未接':
                $Status = '1';
                break;
            case '挂断':
                $Status = '1';
                break;
            default:
                $Status = '0';
                break;
        }

        if ($TalkTime > 0) {
            $Status = '2';
        }

        Db::startTrans();
        try {
            $Data = array(
                'UserID' => $UserID,
                'ClientID' => $ClientID,
                'Status' => $Status,
                'TalkTime' => $TalkTime
            );
            $RecordID = Db::table('user_call_records')->insertGetId($Data);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            mb_OutMessage(ErrCode_Toast, $e->getMessage());
        }
        mb_OutMessage(ErrCode_Normal, '保存成功', array('RecordID' => $RecordID));
    }

    /**
     * 設置語音文件
     */
    public function mb_SetCallVoice()
    {
        $RecordID = Request::has('RecordID') ? Request::param('RecordID') : 0;
        $VoiceUrl = Request::has('VoiceUrl') ? Request::param('VoiceUrl') : '';
        if (!$RecordID || !$VoiceUrl) mb_OutMessage(ErrCode_Toast, '参数有误');

        Db::startTrans();
        try {
            $Data = array(
                'ID' => $RecordID,
                'VoiceUrl' => $VoiceUrl,
            );
            Db::table('user_call_records')->update($Data);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            mb_OutMessage(ErrCode_Toast, $e->getMessage());
        }
        mb_OutMessage(ErrCode_Normal, '保存成功');
    }
}